using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;
using Microsoft.Practices.RecipeFramework;
using Microsoft.Practices.Common;
using EnvDTE;
using EnvDTE80;
using Microsoft.Practices.ComponentModel;
using Microsoft.Practices.RepositoryFactory.CommonHelpers;

namespace Microsoft.Practices.RepositoryFactory.ValueProviders
{
    /// <summary>
    /// A value provider that gives a list of the projects in a solution
    /// that have a particular responsibility assigned. The specific
    /// responsibility that we look for is supplied in the configuration.
    /// </summary>
    [ServiceDependency(typeof(DTE))]
    public class ProjectFoldersProvider : ValueProvider, IAttributesConfigurable
    {
        private const string ProjectArgumentKey = "ProjectArgument";
        private string _projectName = string.Empty;

        /// <summary>
        /// Called when [begin recipe].
        /// </summary>
        /// <param name="currentValue">The current value.</param>
        /// <param name="newValue">The new value.</param>
        /// <returns></returns>
        public override bool OnBeginRecipe(object currentValue, out object newValue)
        {
            return GetValue(out newValue);
        }

        public override bool OnArgumentChanged(string changedArgumentName, object changedArgumentValue, object currentValue, out object newValue)
        {
            return OnBeginRecipe(currentValue, out newValue);
        }

        private bool GetValue(out object newValue)
        {
            IDictionaryService dictservice = (IDictionaryService)
                ServiceHelper.GetService(this, typeof(IDictionaryService));

            Project project = dictservice.GetValue(_projectName) as Project;

            ProjectFolderFinder finder = new ProjectFolderFinder((DTE)(Site.GetService(typeof(DTE))), project);
            List<ProjectItem> projectFolders = finder.GetAllFolders();
            if (projectFolders.Count > 0)
            {
                newValue = (ProjectItem) projectFolders[0];
            }
            else
            {
                newValue = null;
            }
            
            return true;
        }

        #region IAttributesConfigurable Members

        /// <summary>
        /// Configures the specified attributes.
        /// </summary>
        /// <param name="attributes">The attributes.</param>
        public void Configure(System.Collections.Specialized.StringDictionary attributes)
        {
            if (attributes == null)
                throw new ArgumentNullException("attributes");

            if (attributes.ContainsKey(ProjectArgumentKey))
            {
                _projectName = attributes[ProjectArgumentKey];
            }
            else
            {
                
                throw new ArgumentException(Properties.Resources.InvalidArgument, ProjectArgumentKey);
            }

        }

        #endregion
    }
}
